package bb.lanxing.common.engin.util;

import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;

import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.manager.SPConstant;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.util.Log;

public class SettingUtils {
    private static final String CHARSET = "utf-8";
    public static final String SETTING_FILE_NAME = "zx_settings.ini";
    private static final String TAG = "SettingUtils";

    public static boolean loadSetting(String str) {
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(str, SETTING_FILE_NAME));
            Properties properties = new Properties();
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
            properties.load(inputStreamReader);
            SharedManager sharedManager = SharedManager.getInstance();
            RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
            sharedManager.setDisplayMode(Integer.parseInt(properties.getProperty(SharedManager.KEY_SPORT_SETTING_DISPLAY_MODE, "1")));
            sharedManager.setNightMode(toBoolean(properties.getProperty(SharedManager.KEY_SPORT_SETTING_NIGHT_MODE, "false")));
            sharedManager.setBigFontMode(toBoolean(properties.getProperty(SharedManager.KEY_SPORT_SETTING_BIG_FONT_MODE, "false")));
            remoteSharedPreference.setValue(SPConstant.KEY_UPLOAD_LOCATION, toBoolean(properties.getProperty(SPConstant.KEY_UPLOAD_LOCATION, "false")));
            remoteSharedPreference.setValue(SPConstant.KEY_BATTERY_SAVING_MODE, toBoolean(properties.getProperty(SPConstant.KEY_BATTERY_SAVING_MODE, "false")));
            remoteSharedPreference.setValue(SPConstant.KEY_GPS_SOUND_ENABLED, toBoolean(properties.getProperty(SPConstant.KEY_GPS_SOUND_ENABLED, "true")));
            String property = properties.getProperty(SPConstant.KEY_TTS_DISTANCE, "KM1");
            String property2 = properties.getProperty(SPConstant.KEY_TTS_DURATION, "M5");
            remoteSharedPreference.setValue(SPConstant.KEY_TTS_DISTANCE, property);
            remoteSharedPreference.setValue(SPConstant.KEY_TTS_DURATION, property2);
            String property3 = properties.getProperty(SPConstant.KEY_ALTITUDE_SOURCE_V2, String.valueOf(0));
            String property4 = properties.getProperty(SPConstant.KEY_ALTITUDE_ADJUST_VALUE_V2, "0");
            remoteSharedPreference.setValue(SPConstant.KEY_ALTITUDE_SOURCE_V2, property3);
            remoteSharedPreference.setValue(SPConstant.KEY_ALTITUDE_ADJUST_VALUE_V2, property4);
            sharedManager.setSyncQQHealth(toBoolean(properties.getProperty(SharedManager.KEY_USER_SETTING_SYNC_QQ_HEALTH, "false")));
            sharedManager.setAutoUploadMode(Integer.parseInt(properties.getProperty(SharedManager.KEY_USER_SETTING_AUTO_UPLOAD_MODE, String.valueOf(0))));
            remoteSharedPreference.setValue(SPConstant.KEY_APP_AUTO_FRONT, toBoolean(properties.getProperty(SPConstant.KEY_APP_AUTO_FRONT, "true")));
            inputStreamReader.close();
            SharedManager.refresh();
            return true;
        } catch (IOException e) {
            Log.w(TAG, e.getMessage());
            return false;
        }
    }

    public static boolean writeSetting(String str) {
        File file = new File(str);
        if (!file.exists()) {
            file.mkdirs();
        }
        File file2 = new File(file, SETTING_FILE_NAME);
        try {
            if (!file2.exists()) {
                file2.createNewFile();
            }
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file2), "utf-8");
            Properties properties = new Properties();
            SharedManager sharedManager = SharedManager.getInstance();
            RemoteSharedPreference remoteSharedPreference = RemoteSharedPreference.getInstance();
            properties.setProperty(SharedManager.KEY_SPORT_SETTING_DISPLAY_MODE, String.valueOf(sharedManager.getDisplayMode()));
            properties.setProperty(SharedManager.KEY_SPORT_SETTING_NIGHT_MODE, String.valueOf(sharedManager.isNightMode()));
            properties.setProperty(SharedManager.KEY_SPORT_SETTING_BIG_FONT_MODE, String.valueOf(sharedManager.isBigFontMode()));
            properties.setProperty(SPConstant.KEY_UPLOAD_LOCATION, String.valueOf(remoteSharedPreference.getBoolean(SPConstant.KEY_UPLOAD_LOCATION, false)));
            properties.setProperty(SPConstant.KEY_BATTERY_SAVING_MODE, String.valueOf(RemoteSharedPreference.getInstance().getBoolean(SPConstant.KEY_BATTERY_SAVING_MODE, false)));
            properties.setProperty(SPConstant.KEY_GPS_SOUND_ENABLED, String.valueOf(RemoteSharedPreference.getInstance().getGPSSoundEnable()));
            properties.setProperty(SPConstant.KEY_TTS_DISTANCE, RemoteSharedPreference.getInstance().getString(SPConstant.KEY_TTS_DISTANCE, "KM1"));
            properties.setProperty(SPConstant.KEY_TTS_DURATION, RemoteSharedPreference.getInstance().getString(SPConstant.KEY_TTS_DURATION, "M5"));
            properties.setProperty(SPConstant.KEY_ALTITUDE_SOURCE_V2, String.valueOf(RemoteSharedPreference.getInstance().getAltitudeSource()));
            properties.setProperty(SPConstant.KEY_ALTITUDE_ADJUST_VALUE_V2, String.valueOf(RemoteSharedPreference.getInstance().getAltitudeAdjustValue()));
            properties.setProperty(SharedManager.KEY_USER_SETTING_SYNC_QQ_HEALTH, String.valueOf(sharedManager.isSyncQQHealth()));
            properties.setProperty(SharedManager.KEY_USER_SETTING_AUTO_UPLOAD_MODE, String.valueOf(sharedManager.getAutoUploadMode()));
            properties.setProperty(SPConstant.KEY_APP_AUTO_FRONT, String.valueOf(RemoteSharedPreference.getInstance().getBoolean(SPConstant.KEY_APP_AUTO_FRONT, true)));
            properties.setProperty(SharedManager.KEY_IS_FW_TEST_MODE, String.valueOf(sharedManager.isFwTestMode()));
            properties.store(outputStreamWriter, "xing-zhe application settings");
            outputStreamWriter.close();
            return true;
        } catch (IOException e) {
            Log.w(TAG, e.getMessage());
            return false;
        }
    }

    private static boolean toBoolean(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        return Boolean.valueOf(str);
    }
}
